{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Part 2:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Change loss function from $loss=\\frac{1}{n}\\sum{(y_i - \\hat{y_i})^2}$ to $loss = \\frac{1}{n}\\sum{|y_i - \\hat{y_i}|}$,and using your mathmatical knowledge to get the right partial formual.Implemen the gradient descent code."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### $loss = \\frac{1}{n}\\sum{|y_i - \\hat{y_i}|}$\n",
    "### $\\frac{\\partial loss}{\\partial k} = \\frac{1}{n}\\sum{}\\begin{cases} -x_i , y_i > \\hat{y_i}\\\\ x_i , y_i <= \\hat{y_i} \\end{cases}$\n",
    "### $\\frac{\\partial loss}{\\partial b}=\\frac{1}{n}\\sum{}\\begin{cases}-1,y_i > \\hat{y_i}\\\\ 1,y_i <= \\hat{y_i} \\end{cases}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def partial_k(x,y,y_hat):\n",
    "    '''compute the partial of k'''\n",
    "    gradient = 0\n",
    "    \n",
    "    n = len(y)\n",
    "    \n",
    "    for x_i,y_i,y_hat_i in zip(list(x),list(y),list(y_hat)):\n",
    "        gradient = gradient + x_i if y_i <= y_hat_i else gradient - x_i\n",
    "        \n",
    "    return gradient / n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def partial_b(x,y,y_hat):\n",
    "    '''compute the partial of b'''\n",
    "    gradient = 0\n",
    "    \n",
    "    n = len(y)\n",
    "    \n",
    "    for y_i,y_hat_i in zip(list(y),list(y_hat)):\n",
    "        gradient = gradient + 1 if y_i <= y_hat_i else gradient - 1\n",
    "        \n",
    "    return gradient / n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def price(rm,k,b):\n",
    "    '''return k*rm + b'''\n",
    "    return rm * k + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loss(y,y_hat):\n",
    "    n = len(y)\n",
    "    return sum([abs(y_i - y_hat_i) for y_i,y_hat_i in zip(y,y_hat)]) / n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_boston"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = load_boston()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "x,y = data['data'],data['target']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_rm = x[:,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw_rm_and_price():\n",
    "    plt.scatter(x_rm,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXEAAAD6CAYAAABXh3cLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJztnX2QHOWd37+/GTXSLHY0K7OpwERCOK6IRBbSHoutsnwvkmN0CS+1ASuygfJVXopKcq4UmNpEqsJGXORYd1tX4Cu7klB1f1w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ojSZ73Adjhov8DoOgtgm4D8Hzag0iQvWiFAtcDWCMin0t5PEnyj5VSd6Al4n9uutFAi7hS6q7lmOKXAcwopb6b9pgS4usAviwiJQCfBvA/Uh5PkswAsJctfwotIS8ky4/evwHgaMpDSZKPA7iw/PNHAD6W4liS5NcA/DsRWYlWKOW46YYDLeIDxHcB/EMALwN4RilV2PCRUurPAPyFiPx3AGeUUq+kPaYEuQnAz5RSFwI/mV++B+CficifoRViKGro6EcAVgH4rwD+tVLqA9MNudiHEEJyDGfihBCSYyjihBCSYyjihBCSYyjihBCSYyjihBCSYyjihBCSYyjihBCSY/4/yyRDxLhi4zIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "draw_rm_and_price()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "When time is 0,get the best_k:76.50432178961213,and best_b:-98.89616562008597,the loss is 359.37271955582565\n",
      "When time is 50,get the best_k:45.08114985285306,and best_b:-103.89616562008568,the loss is 156.8895726424024\n",
      "When time is 100,get the best_k:20.564279497121742,and best_b:-107.69379407858364,the loss is 7.347944294179368\n",
      "When time is 150,get the best_k:20.50735261965131,and best_b:-107.35782569913721,the loss is 7.324721209906077\n",
      "When time is 200,get the best_k:20.45042574218088,and best_b:-107.02185731969078,the loss is 7.301498125632796\n",
      "When time is 250,get the best_k:20.39857159198318,and best_b:-106.68509842641035,the loss is 7.278279678733929\n",
      "When time is 300,get the best_k:20.344181078149116,and best_b:-106.34873479004692,the loss is 7.255059406474194\n",
      "When time is 350,get the best_k:20.295298864710315,and best_b:-106.01158063984948,the loss is 7.231847744099761\n",
      "When time is 400,get the best_k:20.245426532694456,and best_b:-105.67482174656897,the loss is 7.208670397177282\n",
      "When time is 450,get the best_k:20.196826927951317,and best_b:-105.33924862403943,the loss is 7.185675317856162\n",
      "When time is 500,get the best_k:20.14752811372199,and best_b:-105.00407075842698,the loss is 7.162719246224064\n",
      "When time is 550,get the best_k:20.095977125579623,and best_b:-104.66928814973151,the loss is 7.13976989553277\n",
      "When time is 600,get the best_k:20.041922975381922,and best_b:-104.33490079795304,the loss is 7.116825055003324\n",
      "When time is 650,get the best_k:19.988216651271177,and best_b:-104.00051344617457,the loss is 7.093883278230537\n",
      "When time is 700,get the best_k:19.93281309395891,and best_b:-103.66849763589796,the loss is 7.071239038909224\n",
      "When time is 750,get the best_k:19.883129299492538,and best_b:-103.33687708253841,the loss is 7.048748760901446\n",
      "When time is 800,get the best_k:19.831750248109177,and best_b:-103.00565178609585,the loss is 7.0262779624535865\n",
      "When time is 850,get the best_k:19.78252376589577,and best_b:-102.67442648965326,the loss is 7.0039050396096325\n",
      "When time is 900,get the best_k:19.729451829136867,and best_b:-102.34478222087859,the loss is 6.981552866822261\n",
      "When time is 950,get the best_k:19.675610327160538,and best_b:-102.01592846593795,the loss is 6.959354121388802\n",
      "When time is 1000,get the best_k:19.624483449690114,and best_b:-101.68667945408039,the loss is 6.937140844319186\n",
      "When time is 1050,get the best_k:19.5711917500853,and best_b:-101.35782569913982,the loss is 6.9149503226019435\n",
      "When time is 1100,get the best_k:19.51846012953189,and best_b:-101.02897194419918,the loss is 6.892760784897085\n",
      "When time is 1150,get the best_k:19.46607040621172,and best_b:-100.70169921692649,the loss is 6.870800319703687\n",
      "When time is 1200,get the best_k:19.41565578170576,and best_b:-100.37521700348778,the loss is 6.848976266988968\n",
      "When time is 1250,get the best_k:19.36323325206144,and best_b:-100.04913004696597,the loss is 6.827154768502028\n",
      "When time is 1300,get the best_k:19.311641157199738,and best_b:-99.72304309044414,the loss is 6.805353225574111\n",
      "When time is 1350,get the best_k:19.26065301490724,and best_b:-99.39695613392242,the loss is 6.783568908524947\n",
      "When time is 1400,get the best_k:19.209664872614745,and best_b:-99.0708691774007,the loss is 6.761784591475782\n",
      "When time is 1450,get the best_k:19.16093997142897,and best_b:-98.74478222087899,the loss is 6.74004374378206\n",
      "When time is 1500,get the best_k:19.1126273232076,and best_b:-98.41988103510826,the loss is 6.718502897551861\n",
      "When time is 1550,get the best_k:19.064237995144328,and best_b:-98.09616562008844,the loss is 6.697062243241384\n",
      "When time is 1600,get the best_k:19.01218226391909,and best_b:-97.7760075173216,the loss is 6.676015542323291\n",
      "When time is 1650,get the best_k:18.960985821231386,and best_b:-97.4562446714717,the loss is 6.655040717413511\n",
      "When time is 1700,get the best_k:18.911574754037733,and best_b:-97.1364818256218,the loss is 6.63409214034621\n",
      "When time is 1750,get the best_k:18.86260597933415,and best_b:-96.8171142366891,the loss is 6.613213574931593\n",
      "When time is 1800,get the best_k:18.811909931903337,and best_b:-96.49853716159018,the loss is 6.592409093742698\n",
      "When time is 1850,get the best_k:18.76171348921561,and best_b:-96.17996008649126,the loss is 6.5716006602211525\n",
      "When time is 1900,get the best_k:18.708690564314406,and best_b:-95.86217352522657,the loss is 6.55094674966453\n",
      "When time is 1950,get the best_k:18.660377520836143,and best_b:-95.54399170704487,the loss is 6.530118241816779\n",
      "When time is 2000,get the best_k:18.61395538644877,and best_b:-95.22660040269712,the loss is 6.509554316360338\n",
      "When time is 2050,get the best_k:18.573073568266892,and best_b:-94.91158063985111,the loss is 6.489373495982725\n",
      "When time is 2100,get the best_k:18.532191750085016,and best_b:-94.5965608770051,the loss is 6.469192675605108\n",
      "When time is 2150,get the best_k:18.482717046527647,and best_b:-94.28312214182708,the loss is 6.44905748182688\n",
      "When time is 2200,get the best_k:18.432221789610587,and best_b:-93.97007866356608,the loss is 6.428940828424228\n",
      "When time is 2250,get the best_k:18.38687119672516,and best_b:-93.65822095605617,the loss is 6.409081947803771\n",
      "When time is 2300,get the best_k:18.339179497120387,and best_b:-93.34794427621416,the loss is 6.389382137480206\n",
      "When time is 2350,get the best_k:18.29291862755515,and best_b:-93.03766759637219,the loss is 6.3697008286744214\n",
      "When time is 2400,get the best_k:18.24411586075671,and best_b:-92.72778617344721,the loss is 6.350008519284327\n",
      "When time is 2450,get the best_k:18.200802026764602,and best_b:-92.41750949360524,the loss is 6.330554728372799\n",
      "When time is 2500,get the best_k:18.15025182913615,and best_b:-92.11118538293323,the loss is 6.311098718112972\n",
      "When time is 2550,get the best_k:18.105706374590685,and best_b:-91.80644229992926,the loss is 6.292152542662106\n",
      "When time is 2600,get the best_k:18.062774754037346,and best_b:-91.50486127226134,the loss is 6.273602901585849\n",
      "When time is 2650,get the best_k:18.015581868661855,and best_b:-91.20446601534425,the loss is 6.2550976794847655\n",
      "When time is 2700,get the best_k:17.968300050480003,and best_b:-90.90486127226116,the loss is 6.236709296048513\n",
      "When time is 2750,get the best_k:17.919468825183532,and best_b:-90.60604704301214,the loss is 6.218382807065817\n",
      "When time is 2800,get the best_k:17.868948667080723,and best_b:-90.30920909834822,the loss is 6.200239547254245\n",
      "When time is 2850,get the best_k:17.820079101863335,and best_b:-90.01237115368417,the loss is 6.18217197678367\n",
      "When time is 2900,get the best_k:17.775316651270494,and best_b:-89.71513795210305,the loss is 6.164062609959153\n",
      "When time is 2950,get the best_k:17.726392145341652,and best_b:-89.41909052127315,the loss is 6.14609169893512\n",
      "When time is 3000,get the best_k:17.684173568266512,and best_b:-89.12462411811117,the loss is 6.12841370437274\n",
      "When time is 3050,get the best_k:17.638436018859327,and best_b:-88.83173874261715,the loss is 6.110832222253035\n",
      "When time is 3100,get the best_k:17.597652224392895,and best_b:-88.54003913787403,the loss is 6.0934888808244025\n",
      "When time is 3150,get the best_k:17.55830163150753,and best_b:-88.25189684538388,the loss is 6.076575469697617\n",
      "When time is 3200,get the best_k:17.514531275776292,and best_b:-87.97047392048258,the loss is 6.060400613431784\n",
      "When time is 3250,get the best_k:17.470994912139886,and best_b:-87.69142253708317,the loss is 6.044444256121857\n",
      "When time is 3300,get the best_k:17.424932066290054,and best_b:-87.41316166751787,the loss is 6.0285951432343925\n",
      "When time is 3350,get the best_k:17.38568582123079,and best_b:-87.13490079795251,the loss is 6.012738421972679\n",
      "When time is 3400,get the best_k:17.34087435878022,and best_b:-86.85940672680611,the loss is 5.997179620976867\n",
      "When time is 3450,get the best_k:17.298413489215047,and best_b:-86.58628419716166,the loss is 5.981901060975914\n",
      "When time is 3500,get the best_k:17.256228508977905,and best_b:-86.31395218135134,the loss is 5.966712424685003\n",
      "When time is 3550,get the best_k:17.21607910186322,and best_b:-86.04280593629187,the loss is 5.951688209938628\n",
      "When time is 3600,get the best_k:17.170624161151686,and best_b:-85.77324071890042,the loss is 5.936748830122496\n",
      "When time is 3650,get the best_k:17.127713884471774,and best_b:-85.50328024459199,the loss is 5.921799882558479\n",
      "When time is 3700,get the best_k:17.08242653269305,and best_b:-85.23371502720056,the loss is 5.906849554692461\n",
      "When time is 3750,get the best_k:17.0398071651041,and best_b:-84.9641498098093,the loss is 5.891963966106105\n",
      "When time is 3800,get the best_k:16.99896289632937,and best_b:-84.69577036316892,the loss is 5.87723373012513\n",
      "When time is 3850,get the best_k:16.963953410163374,and best_b:-84.42857668727947,the loss is 5.862694967068363\n",
      "When time is 3900,get the best_k:16.921839971428223,and best_b:-84.16494032364307,the loss is 5.84846455723813\n",
      "When time is 3950,get the best_k:16.886567244155493,and best_b:-83.90328024459153,the loss is 5.834513158192014\n",
      "When time is 4000,get the best_k:16.856328113720714,and best_b:-83.6424106793739,the loss is 5.820746417456975\n",
      "When time is 4050,get the best_k:16.82026329158636,and best_b:-83.38312214182433,the loss is 5.807012631087838\n",
      "When time is 4100,get the best_k:16.78110597933338,and best_b:-83.12857668727887,the loss is 5.7937515241171305\n",
      "When time is 4150,get the best_k:16.74039175008434,and best_b:-82.87482174656742,the loss is 5.780542165202765\n",
      "When time is 4200,get the best_k:16.699677520835298,and best_b:-82.62106680585597,the loss is 5.767332806288395\n",
      "When time is 4250,get the best_k:16.662864872613923,and best_b:-82.3692881497294,the loss is 5.7543921351200895\n",
      "When time is 4300,get the best_k:16.621096493167283,and best_b:-82.12146206277278,the loss is 5.741750522654647\n",
      "When time is 4350,get the best_k:16.580479101862945,and best_b:-81.8736359758161,the loss is 5.7291407043174996\n",
      "When time is 4400,get the best_k:16.540688192772077,and best_b:-81.62620514577647,the loss is 5.716578760797837\n",
      "When time is 4450,get the best_k:16.50089728368121,and best_b:-81.37877431573683,the loss is 5.704016817278175\n",
      "When time is 4500,get the best_k:16.46110637459034,and best_b:-81.1313434856972,the loss is 5.691454873758507\n",
      "When time is 4550,get the best_k:16.42131546549947,and best_b:-80.88391265565757,the loss is 5.678892930238843\n",
      "When time is 4600,get the best_k:16.381524556408603,and best_b:-80.63648182561793,the loss is 5.66633098671918\n",
      "When time is 4650,get the best_k:16.341733647317735,and best_b:-80.3890509955783,the loss is 5.653769043199514\n",
      "When time is 4700,get the best_k:16.300300445736763,and best_b:-80.14241067937266,the loss is 5.641245738562457\n",
      "When time is 4750,get the best_k:16.26274590028228,and best_b:-79.89537510625003,the loss is 5.628786746848519\n",
      "When time is 4800,get the best_k:16.22371941806891,and best_b:-79.64913004696142,the loss is 5.616349488172527\n",
      "When time is 4850,get the best_k:16.1849407619425,and best_b:-79.40328024458975,the loss is 5.603952216702886\n",
      "When time is 4900,get the best_k:16.14791230344455,and best_b:-79.1574304422182,the loss is 5.591599363382907\n",
      "When time is 4950,get the best_k:16.10928344968968,and best_b:-78.91197589676368,the loss is 5.579241856602837\n",
      "When time is 5000,get the best_k:16.073124161152176,and best_b:-78.66652135130902,the loss is 5.566938898849896\n",
      "When time is 5050,get the best_k:16.03445696747627,and best_b:-78.42225257660543,the loss is 5.554742894513052\n",
      "When time is 5100,get the best_k:16.000483844946555,and best_b:-78.17798380190172,the loss is 5.542552518615809\n",
      "When time is 5150,get the best_k:15.964094516883328,and best_b:-77.93411028411498,the loss is 5.530396334005041\n",
      "When time is 5200,get the best_k:15.92728779751575,and best_b:-77.6906320232453,the loss is 5.5182949690279095\n",
      "When time is 5250,get the best_k:15.884254595934703,and best_b:-77.4499205607945,the loss is 5.5063104304646675\n",
      "When time is 5300,get the best_k:15.846539576171836,and best_b:-77.20881384142666,the loss is 5.494402504605009\n",
      "When time is 5350,get the best_k:15.810071196725174,and best_b:-76.9677071220588,the loss is 5.482510181459035\n",
      "When time is 5400,get the best_k:15.775495307397088,and best_b:-76.72660040269096,the loss is 5.4706415443603325\n",
      "When time is 5450,get the best_k:15.746336018859525,and best_b:-76.48549368332318,the loss is 5.458890953854988\n",
      "When time is 5500,get the best_k:15.710239971428715,and best_b:-76.24794427620836,the loss is 5.447291024759465\n",
      "When time is 5550,get the best_k:15.675407955618466,and best_b:-76.01079012601042,the loss is 5.435799952703169\n",
      "When time is 5600,get the best_k:15.640575939808217,and best_b:-75.77363597581248,the loss is 5.424308880646885\n",
      "When time is 5650,get the best_k:15.605743923997968,and best_b:-75.53648182561454,the loss is 5.412817808590594\n",
      "When time is 5700,get the best_k:15.57091190818772,and best_b:-75.2993276754166,the loss is 5.401326736534313\n",
      "When time is 5750,get the best_k:15.533585030717392,and best_b:-75.06256878213567,the loss is 5.389837031629516\n",
      "When time is 5800,get the best_k:15.496298864709514,and best_b:-74.82580988885474,the loss is 5.378347610337182\n",
      "When time is 5850,get the best_k:15.463187007002034,and best_b:-74.58944625249099,the loss is 5.3670470124087855\n",
      "When time is 5900,get the best_k:15.421876730322227,and best_b:-74.35624467146322,the loss is 5.355822961444223\n",
      "When time is 5950,get the best_k:15.38629925996653,and best_b:-74.12264783351856,the loss is 5.344633542312041\n",
      "When time is 6000,get the best_k:15.3525628963302,and best_b:-73.88905099557373,the loss is 5.333473342308654\n",
      "When time is 6050,get the best_k:15.316960129531816,and best_b:-73.65584941454581,the loss is 5.322345484253526\n",
      "When time is 6100,get the best_k:15.280044714512075,and best_b:-73.42383360426895,the loss is 5.311342895023104\n",
      "When time is 6150,get the best_k:15.240010722417217,and best_b:-73.1977466477472,the loss is 5.300759681911785\n",
      "When time is 6200,get the best_k:15.204981868662294,and best_b:-72.97205494814233,the loss is 5.290323485081395\n",
      "When time is 6250,get the best_k:15.169953014907371,and best_b:-72.74636324853746,the loss is 5.279898351365038\n",
      "When time is 6300,get the best_k:15.13671507024335,and best_b:-72.52225257660078,the loss is 5.269706373975967\n",
      "When time is 6350,get the best_k:15.098992145342173,and best_b:-72.30209447383407,the loss is 5.259654909393714\n",
      "When time is 6400,get the best_k:15.065654200678138,and best_b:-72.08549368332028,the loss is 5.250076088796947\n",
      "When time is 6450,get the best_k:15.034479101863914,and best_b:-71.87165969122549,the loss is 5.240732916962663\n",
      "When time is 6500,get the best_k:15.00409846945285,and best_b:-71.6578256991307,the loss is 5.2314052257933135\n",
      "When time is 6550,get the best_k:14.97034076194297,and best_b:-71.44478222086991,the loss is 5.222101917048592\n",
      "When time is 6600,get the best_k:14.933659734274974,and best_b:-71.23292451336013,the loss is 5.212850044278364\n",
      "When time is 6650,get the best_k:14.895296097911324,and best_b:-71.02146206276736,the loss is 5.2036227918523945\n",
      "When time is 6700,get the best_k:14.864284635460718,and best_b:-70.81039486909158,the loss is 5.194524546145853\n",
      "When time is 6750,get the best_k:14.834251829136614,and best_b:-70.60130396000082,the loss is 5.185608813521355\n",
      "When time is 6800,get the best_k:14.810996888425153,and best_b:-70.39142253707605,the loss is 5.176720116557827\n",
      "When time is 6850,get the best_k:14.779802422021975,and best_b:-70.18272688490228,the loss is 5.167794680059773\n",
      "When time is 6900,get the best_k:14.750722975381668,and best_b:-69.97600751731319,the loss is 5.159090920266949\n",
      "When time is 6950,get the best_k:14.725139180915296,and best_b:-69.77126443430927,the loss is 5.150537788611956\n",
      "When time is 7000,get the best_k:14.692122184867848,and best_b:-69.57126443430957,the loss is 5.142324469891941\n",
      "When time is 7050,get the best_k:14.656829694749254,and best_b:-69.37165969122687,the loss is 5.1341055345312965\n",
      "When time is 7100,get the best_k:14.621537204630659,and best_b:-69.17205494814417,the loss is 5.125886599170658\n",
      "When time is 7150,get the best_k:14.588666453642498,and best_b:-68.97205494814446,the loss is 5.117674645500878\n",
      "When time is 7200,get the best_k:14.55410518882034,and best_b:-68.77245020506176,the loss is 5.109462535391368\n",
      "When time is 7250,get the best_k:14.521965663128618,and best_b:-68.57245020506205,the loss is 5.101257406972721\n",
      "When time is 7300,get the best_k:14.489826137436896,and best_b:-68.37245020506235,the loss is 5.093052278554075\n",
      "When time is 7350,get the best_k:14.457686611745174,and best_b:-68.17245020506265,the loss is 5.084847150135429\n",
      "When time is 7400,get the best_k:14.427318627555426,and best_b:-67.97245020506294,the loss is 5.076658557271156\n",
      "When time is 7450,get the best_k:14.399667639413108,and best_b:-67.77205494814623,the loss is 5.068479702023465\n",
      "When time is 7500,get the best_k:14.36929965522336,and best_b:-67.57205494814653,the loss is 5.0602911091591825\n",
      "When time is 7550,get the best_k:14.337741157199618,and best_b:-67.37284546198067,the loss is 5.052182779090609\n",
      "When time is 7600,get the best_k:14.309202422021722,and best_b:-67.17521700348294,the loss is 5.044180118539686\n",
      "When time is 7650,get the best_k:14.276938785658055,and best_b:-66.97837905881923,the loss is 5.03622327776965\n",
      "When time is 7700,get the best_k:14.24467514929439,and best_b:-66.78154111415552,the loss is 5.028266436999618\n",
      "When time is 7750,get the best_k:14.212411512930723,and best_b:-66.5847031694918,the loss is 5.020309596229577\n",
      "When time is 7800,get the best_k:14.17966329158684,and best_b:-66.38826048174495,the loss is 5.012428353717289\n",
      "When time is 7850,get the best_k:14.152349852851668,and best_b:-66.19181779399804,the loss is 5.004515676846005\n",
      "When time is 7900,get the best_k:14.122545505025569,and best_b:-65.99616562008529,the loss is 4.996683964309497\n",
      "When time is 7950,get the best_k:14.09323997142872,and best_b:-65.80051344617254,the loss is 4.988853899042898\n",
      "When time is 8000,get the best_k:14.059864477357564,and best_b:-65.60565178609377,the loss is 4.981041638860748\n",
      "When time is 8050,get the best_k:14.029834042574947,and best_b:-65.410394869098,the loss is 4.973230508513631\n",
      "When time is 8100,get the best_k:14.000877520835822,and best_b:-65.21750949360401,the loss is 4.965658460009973\n",
      "When time is 8150,get the best_k:13.967082263918838,and best_b:-65.0266004026949,the loss is 4.958105364887412\n",
      "When time is 8200,get the best_k:13.935696097910945,and best_b:-64.83529605486879,the loss is 4.95059251853065\n",
      "When time is 8250,get the best_k:13.904343923997914,and best_b:-64.64399170704269,the loss is 4.943080438473942\n",
      "When time is 8300,get the best_k:13.873059734274602,and best_b:-64.45268735921661,the loss is 4.935569891017327\n",
      "When time is 8350,get the best_k:13.839672382495937,and best_b:-64.26177826830765,the loss is 4.928061466187076\n",
      "When time is 8400,get the best_k:13.808864082100662,and best_b:-64.07047392048177,the loss is 4.920547703227965\n",
      "When time is 8450,get the best_k:13.778055781705387,and best_b:-63.87916957265574,the loss is 4.913033940268849\n",
      "When time is 8500,get the best_k:13.747247481310112,and best_b:-63.68786522482962,the loss is 4.9055265337277385\n",
      "When time is 8550,get the best_k:13.721802817278501,and best_b:-63.498141904671485,the loss is 4.8982038715825675\n",
      "When time is 8600,get the best_k:13.693096493167838,and best_b:-63.311185382932315,the loss is 4.8910378698165236\n",
      "When time is 8650,get the best_k:13.661357757989961,and best_b:-63.12580988886113,the loss is 4.8839677280388365\n",
      "When time is 8700,get the best_k:13.631894912140165,and best_b:-62.94043439478994,the loss is 4.876938840095963\n",
      "When time is 8750,get the best_k:13.600334833088768,and best_b:-62.75663992838673,the loss is 4.869954541343471\n",
      "When time is 8800,get the best_k:13.569754991191532,and best_b:-62.57363597581752,the loss is 4.863078807262466\n",
      "When time is 8850,get the best_k:13.545881868661894,and best_b:-62.39339882166727,the loss is 4.856455696482822\n",
      "When time is 8900,get the best_k:13.516038785657958,and best_b:-62.21750949360397,the loss is 4.850091577565607\n",
      "When time is 8950,get the best_k:13.490562501072967,and best_b:-62.04359645012565,the loss is 4.8439095433084605\n",
      "When time is 9000,get the best_k:13.46191427972907,and best_b:-61.8716596912323,the loss is 4.837844407097287\n",
      "When time is 9050,get the best_k:13.43439491214014,and best_b:-61.70011818925595,the loss is 4.831798921199986\n",
      "When time is 9100,get the best_k:13.409585030717217,and best_b:-61.52818143036261,the loss is 4.82580709670066\n",
      "When time is 9150,get the best_k:13.381639180914831,and best_b:-61.35703518530325,the loss is 4.819792271733997\n",
      "When time is 9200,get the best_k:13.350247876566995,and best_b:-61.18667945407789,the loss is 4.813751758935299\n",
      "When time is 9250,get the best_k:13.320468429926684,and best_b:-61.017114236686524,the loss is 4.807882476111299\n",
      "When time is 9350,get the best_k:13.266920603839724,and best_b:-60.68233162799094,the loss is 4.796362797898568\n",
      "When time is 9400,get the best_k:13.239393726369359,and best_b:-60.51750949360351,the loss is 4.790836208182272\n",
      "When time is 9450,get the best_k:13.215675939808087,and best_b:-60.353477873050075,the loss is 4.78524826933219\n",
      "When time is 9500,get the best_k:13.187809536646025,and best_b:-60.19023676633063,the loss is 4.779761956246532\n",
      "When time is 9550,get the best_k:13.163872777752749,and best_b:-60.02778617344517,the loss is 4.77435775863528\n",
      "When time is 9600,get the best_k:13.139489773800198,and best_b:-59.86731186514469,the loss is 4.769100029172001\n",
      "When time is 9650,get the best_k:13.112883054432631,and best_b:-59.707628070678204,the loss is 4.763857498727054\n",
      "When time is 9700,get the best_k:13.086276335065063,and best_b:-59.547944276211716,the loss is 4.75861496828212\n",
      "When time is 9750,get the best_k:13.062979892377337,and best_b:-59.38786522482823,the loss is 4.753383981623987\n",
      "When time is 9800,get the best_k:13.043245109768636,and best_b:-59.23292451336573,the loss is 4.748568241393852\n",
      "When time is 9850,get the best_k:13.019413093958365,and best_b:-59.08430791257517,the loss is 4.744006361915775\n",
      "When time is 9900,get the best_k:12.99207001095441,and best_b:-58.9400391378715,the loss is 4.739686705055606\n",
      "When time is 10000,get the best_k:12.947107165104601,and best_b:-58.65229210229841,the loss is 4.7312614620229265\n",
      "When time is 10050,get the best_k:12.924135623602638,and best_b:-58.5092090983459,the loss is 4.727100250307388\n",
      "When time is 10100,get the best_k:12.897303212535448,and best_b:-58.36731186514429,the loss is 4.722837393606489\n",
      "When time is 10150,get the best_k:12.872124161152048,and best_b:-58.22580988885959,the loss is 4.718718116881963\n",
      "When time is 10200,get the best_k:12.84827949712043,and best_b:-58.084307912574886,the loss is 4.714595112721754\n",
      "When time is 10250,get the best_k:12.824560524788414,and best_b:-57.942805936290185,the loss is 4.710476749689445\n",
      "When time is 10300,get the best_k:12.801673173009757,and best_b:-57.801303960005484,the loss is 4.706368020343346\n",
      "When time is 10350,get the best_k:12.780653805420823,and best_b:-57.659801983720826,the loss is 4.702275112081221\n",
      "When time is 10400,get the best_k:12.754254595934656,and best_b:-57.51988103510423,the loss is 4.698274438932638\n",
      "When time is 10450,get the best_k:12.736509536646132,and best_b:-57.38035534340462,the loss is 4.6943073047133845\n",
      "When time is 10500,get the best_k:12.714241947713344,and best_b:-57.24280593629003,the loss is 4.690406183714439\n",
      "When time is 10550,get the best_k:12.693263686843807,and best_b:-57.10683755684327,the loss is 4.6866408872522225\n",
      "When time is 10600,get the best_k:12.666873568266757,and best_b:-56.97205494814754,the loss is 4.682894141216528\n",
      "When time is 10750,get the best_k:12.609285425974338,and best_b:-56.574031232732594,the loss is 4.672153518068073\n",
      "When time is 10800,get the best_k:12.58662653269371,and best_b:-56.44399170704094,the loss is 4.668590029418084\n",
      "When time is 10850,get the best_k:12.564575939808336,and best_b:-56.31553320901713,the loss is 4.665213873717108\n",
      "When time is 10900,get the best_k:12.545317046527709,and best_b:-56.18707471099328,the loss is 4.66182574640298\n",
      "When time is 10950,get the best_k:12.526305584077122,and best_b:-56.05861621296944,the loss is 4.658474215234902\n",
      "When time is 11000,get the best_k:12.506722184867643,and best_b:-55.93094822877969,the loss is 4.65524580947019\n",
      "When time is 11050,get the best_k:12.486137204630479,and best_b:-55.80565178609198,the loss is 4.652027711577701\n",
      "When time is 11150,get the best_k:12.442702026764827,and best_b:-55.5578256991355,the loss is 4.645566839627144\n",
      "When time is 11200,get the best_k:12.419179497120549,and best_b:-55.435691311783636,the loss is 4.642449927410922\n",
      "When time is 11300,get the best_k:12.388141552456496,and best_b:-55.19379407858202,the loss is 4.636978152188721\n",
      "When time is 11350,get the best_k:12.364065663128414,and best_b:-55.079169572653186,the loss is 4.633858542970833\n",
      "When time is 11400,get the best_k:12.345369220440638,and best_b:-54.96573083747536,the loss is 4.63120981988345\n",
      "When time is 11450,get the best_k:12.325811512930724,and best_b:-54.8526873592145,the loss is 4.6285623307652655\n",
      "When time is 11500,get the best_k:12.307917837041359,and best_b:-54.739643880953565,the loss is 4.6259439441766625\n",
      "When time is 11550,get the best_k:12.290559339017612,and best_b:-54.62660040269261,the loss is 4.623323688696183\n",
      "When time is 11600,get the best_k:12.272020999096638,and best_b:-54.513952181348664,the loss is 4.620718627792886\n",
      "When time is 11650,get the best_k:12.253771987238906,and best_b:-54.40130396000473,the loss is 4.61812166374877\n",
      "When time is 11700,get the best_k:12.233142342970108,and best_b:-54.2890509955778,the loss is 4.615519070013645\n",
      "When time is 11750,get the best_k:12.216053410163788,and best_b:-54.17679803115095,the loss is 4.612918563862819\n",
      "When time is 11800,get the best_k:12.197890169057072,and best_b:-54.06573083747509,the loss is 4.610413289846752\n",
      "When time is 11850,get the best_k:12.179624161151919,and best_b:-53.95624467146719,the loss is 4.608018681864287\n",
      "When time is 11950,get the best_k:12.146821394353502,and best_b:-53.74201542245539,the loss is 4.6033170767949025\n",
      "When time is 12000,get the best_k:12.13372376589503,and best_b:-53.63766759636852,the loss is 4.60111574626373\n",
      "When time is 12050,get the best_k:12.118538390401,and best_b:-53.53450554103264,the loss is 4.598948130407802\n",
      "When time is 12200,get the best_k:12.067920603839728,and best_b:-53.2368770825346,the loss is 4.592916069236539\n",
      "When time is 12250,get the best_k:12.053963686843714,and best_b:-53.14122490862162,the loss is 4.5910200971472666\n",
      "When time is 12300,get the best_k:12.039366848899077,and best_b:-53.045967991625645,the loss is 4.589183495364709\n",
      "When time is 12350,get the best_k:12.021980287634273,and best_b:-52.95150158846362,the loss is 4.587321725274976\n",
      "When time is 12400,get the best_k:12.006471987239028,and best_b:-52.857035185301676,the loss is 4.585496942431946\n",
      "When time is 12450,get the best_k:11.992158943760774,and best_b:-52.76256878213978,the loss is 4.583668911608151\n",
      "When time is 12500,get the best_k:11.97784590028252,and best_b:-52.668102378977885,the loss is 4.581840880784373\n",
      "When time is 12550,get the best_k:11.963532856804267,and best_b:-52.57363597581599,the loss is 4.580012849960578\n",
      "When time is 12600,get the best_k:11.949219813326014,and best_b:-52.479169572654094,the loss is 4.578184819136792\n",
      "When time is 12650,get the best_k:11.936232856804288,and best_b:-52.38470316949216,the loss is 4.576393117909204\n",
      "When time is 12700,get the best_k:11.921090564314197,and best_b:-52.29102728016415,the loss is 4.574569160114212\n",
      "When time is 12750,get the best_k:11.905268429926865,and best_b:-52.19774664775315,the loss is 4.572794724148707\n",
      "When time is 12800,get the best_k:11.892098469452554,and best_b:-52.10446601534222,the loss is 4.5710154715650555\n",
      "When time is 12850,get the best_k:11.879724161152149,and best_b:-52.01118538293131,the loss is 4.569238425461389\n",
      "When time is 12900,get the best_k:11.863767244156078,and best_b:-51.92027629202236,the loss is 4.567527484817889\n",
      "When time is 12950,get the best_k:11.84848898328649,and best_b:-51.83134348569837,the loss is 4.5658958362665265\n",
      "When time is 13000,get the best_k:11.834858943760784,and best_b:-51.7432011932084,the loss is 4.564306101251141\n",
      "When time is 13050,get the best_k:11.821228904235078,and best_b:-51.65505890071843,the loss is 4.562716366235748\n",
      "When time is 13100,get the best_k:11.807598864709371,and best_b:-51.56691660822846,the loss is 4.561126631220368\n",
      "When time is 13150,get the best_k:11.793968825183665,and best_b:-51.47877431573849,the loss is 4.559536896204981\n",
      "When time is 13200,get the best_k:11.780338785657959,and best_b:-51.39063202324852,the loss is 4.5579471611895945\n",
      "When time is 13250,get the best_k:11.766708746132252,and best_b:-51.30248973075855,the loss is 4.556357426174211\n",
      "When time is 13300,get the best_k:11.753078706606546,and best_b:-51.21434743826858,the loss is 4.55476769115882\n",
      "When time is 13350,get the best_k:11.73944866708084,and best_b:-51.12620514577861,the loss is 4.553177956143435\n",
      "When time is 13400,get the best_k:11.725818627555133,and best_b:-51.03806285328864,the loss is 4.5515882211280525\n",
      "When time is 13450,get the best_k:11.712188588029427,and best_b:-50.949920560798674,the loss is 4.549998486112664\n",
      "When time is 13500,get the best_k:11.698558548503721,and best_b:-50.861778268308704,the loss is 4.548408751097278\n",
      "When time is 13550,get the best_k:11.684928508978015,and best_b:-50.773635975818735,the loss is 4.546819016081891\n",
      "When time is 13600,get the best_k:11.668762105815944,and best_b:-50.68588894024576,the loss is 4.545228440618723\n",
      "When time is 13650,get the best_k:11.655132066290237,and best_b:-50.59774664775579,the loss is 4.543636024461626\n",
      "When time is 13700,get the best_k:11.641502026764531,and best_b:-50.50960435526582,the loss is 4.542043608304521\n",
      "When time is 13750,get the best_k:11.627871987238825,and best_b:-50.42146206277585,the loss is 4.540451192147422\n",
      "When time is 13800,get the best_k:11.614241947713118,and best_b:-50.33331977028588,the loss is 4.538858775990324\n",
      "When time is 13850,get the best_k:11.600611908187412,and best_b:-50.24517747779591,the loss is 4.537266359833228\n",
      "When time is 13900,get the best_k:11.586981868661706,and best_b:-50.15703518530594,the loss is 4.535673943676124\n",
      "When time is 13950,get the best_k:11.575074754037185,and best_b:-50.06889289281595,the loss is 4.534095677028024\n",
      "When time is 14000,get the best_k:11.562510327159703,and best_b:-49.9827268849108,the loss is 4.532552525280617\n",
      "When time is 14050,get the best_k:11.549501236250604,and best_b:-49.897746647756705,the loss is 4.531095138523959\n",
      "When time is 14100,get the best_k:11.536036414116207,and best_b:-49.813952181353685,the loss is 4.5296538514533955\n",
      "When time is 14150,get the best_k:11.522723765894845,and best_b:-49.730157714950664,the loss is 4.528213880717246\n",
      "When time is 14200,get the best_k:11.509411117673483,and best_b:-49.646363248547644,the loss is 4.526773909981101\n",
      "When time is 14250,get the best_k:11.496098469452122,and best_b:-49.56256878214462,the loss is 4.52533393924496\n",
      "When time is 14300,get the best_k:11.48278582123076,and best_b:-49.4787743157416,the loss is 4.523893968508815\n",
      "When time is 14350,get the best_k:11.469473173009398,and best_b:-49.39497984933858,the loss is 4.522453997772668\n",
      "When time is 14400,get the best_k:11.456171591981711,and best_b:-49.311185382935555,the loss is 4.521014122769936\n",
      "When time is 14450,get the best_k:11.442914279728726,and best_b:-49.2273909165325,the loss is 4.519574630700853\n",
      "When time is 14500,get the best_k:11.429712303444116,and best_b:-49.14359645012941,the loss is 4.51813561729883\n",
      "When time is 14550,get the best_k:11.416623765894684,and best_b:-49.059801983726324,the loss is 4.516697585164293\n",
      "When time is 14600,get the best_k:11.40359609791047,and best_b:-48.976007517323254,the loss is 4.515260079563515\n",
      "When time is 14650,get the best_k:11.390720603839299,and best_b:-48.89221305092022,the loss is 4.513823890297164\n",
      "When time is 14700,get the best_k:11.377905979333345,and best_b:-48.8084185845172,the loss is 4.512388227564579\n",
      "When time is 14750,get the best_k:11.365091354827392,and best_b:-48.72462411811418,the loss is 4.510952564832003\n",
      "When time is 14800,get the best_k:11.352276730321439,and best_b:-48.64082965171116,the loss is 4.509516902099416\n",
      "When time is 14850,get the best_k:11.339462105815485,and best_b:-48.55703518530814,the loss is 4.5080812393668355\n",
      "When time is 14900,get the best_k:11.326647481309532,and best_b:-48.47324071890512,the loss is 4.50664557663426\n",
      "When time is 14950,get the best_k:11.313832856803579,and best_b:-48.3894462525021,the loss is 4.505209913901672\n",
      "When time is 15000,get the best_k:11.300218627554566,and best_b:-48.306047043016015,the loss is 4.503773583551258\n",
      "When time is 15150,get the best_k:11.26809846945183,and best_b:-48.06533558056537,the loss is 4.500040349795955\n",
      "When time is 15450,get the best_k:11.193557362732454,and best_b:-47.620671548944834,the loss is 4.4930612346819565\n",
      "When time is 15600,get the best_k:11.161857757989317,and best_b:-47.41197589677092,the loss is 4.490099254962952\n",
      "When time is 15650,get the best_k:11.152508350874676,and best_b:-47.34675850546668,the loss is 4.489238326332603\n",
      "When time is 15700,get the best_k:11.141014674985332,and best_b:-47.28193637107944,the loss is 4.488369873568733\n",
      "When time is 15900,get the best_k:11.106553410163198,and best_b:-47.03608656870821,the loss is 4.485294961596905\n",
      "When time is 15950,get the best_k:11.096615860756062,and best_b:-46.977193288075895,the loss is 4.4845846822410245\n",
      "When time is 16000,get the best_k:11.087014674985296,and best_b:-46.91869526436058,the loss is 4.483880878334545\n",
      "When time is 16050,get the best_k:11.076363686842997,and best_b:-46.86059249756226,the loss is 4.4831806513000725\n",
      "When time is 16100,get the best_k:11.068116256012965,and best_b:-46.80248973076393,the loss is 4.4825157565434495\n",
      "When time is 16150,get the best_k:11.057986611744216,and best_b:-46.74517747779956,the loss is 4.481824048043088\n",
      "When time is 16200,get the best_k:11.04907001095372,and best_b:-46.68786522483517,the loss is 4.4811609179840355\n",
      "When time is 16250,get the best_k:11.038407165103939,and best_b:-46.631343485704825,the loss is 4.480496889778802\n",
      "When time is 16300,get the best_k:11.030896888424119,and best_b:-46.57442648965749,the loss is 4.479836023252043\n",
      "When time is 16350,get the best_k:11.021140366685012,and best_b:-46.51790475052715,the loss is 4.479174528080855\n",
      "When time is 16400,get the best_k:11.013909141388593,and best_b:-46.46098775447982,the loss is 4.478539055123348\n",
      "When time is 16500,get the best_k:10.995909141388621,and best_b:-46.34794427621914,the loss is 4.477221363595595\n",
      "When time is 16550,get the best_k:10.987115465499302,and best_b:-46.2914225370888,the loss is 4.476567378680969\n",
      "When time is 16650,get the best_k:10.968186611744379,and best_b:-46.17956482957911,the loss is 4.475252278757329\n",
      "When time is 16800,get the best_k:10.940537204629818,and best_b:-46.01671897977698,the loss is 4.47345975825452\n",
      "When time is 16950,get the best_k:10.9157964931674,and best_b:-45.85980198372982,the loss is 4.471764127538358\n",
      "When time is 17000,get the best_k:10.909051038621932,and best_b:-45.80841858452042,the loss is 4.471242577443898\n",
      "When time is 17050,get the best_k:10.900718232297818,and best_b:-45.75782569914498,the loss is 4.470704552622468\n",
      "When time is 17100,get the best_k:10.892666848898608,and best_b:-45.70841858452055,the loss is 4.470209839652417\n",
      "When time is 17150,get the best_k:10.884139971428253,and best_b:-45.659406726813124,the loss is 4.469712100830534\n",
      "When time is 17200,get the best_k:10.875879497119952,and best_b:-45.6103948691057,the loss is 4.469217807875635\n",
      "When time is 17250,get the best_k:10.867246295538921,and best_b:-45.561778268315265,the loss is 4.468721818335105\n",
      "When time is 17350,get the best_k:10.850793331112028,and best_b:-45.4649403236514,the loss is 4.467756625957369\n",
      "When time is 17600,get the best_k:10.810568825183113,and best_b:-45.22660040270295,the loss is 4.465419118102927\n",
      "When time is 17650,get the best_k:10.80442653269298,and best_b:-45.17996008649748,the loss is 4.4649990452202815\n",
      "When time is 17750,get the best_k:10.789103607791823,and best_b:-45.08865573867155,the loss is 4.464140032288054\n",
      "When time is 17850,get the best_k:10.773837599886742,and best_b:-44.998141904679606,the loss is 4.463293359022582\n",
      "When time is 17900,get the best_k:10.767422580123924,and best_b:-44.95268735922514,the loss is 4.462878923700989\n",
      "When time is 17950,get the best_k:10.761184240203004,and best_b:-44.90723281377067,the loss is 4.462466006219853\n",
      "When time is 18050,get the best_k:10.744247876566702,and best_b:-44.81711423669572,the loss is 4.461609351013932\n",
      "When time is 18100,get the best_k:10.738245109768313,and best_b:-44.771659691241254,the loss is 4.461197213946891\n",
      "When time is 18150,get the best_k:10.729806374590478,and best_b:-44.72660040270378,the loss is 4.4607687649283365\n",
      "When time is 18250,get the best_k:10.71571823229805,and best_b:-44.63608656871184,the loss is 4.459931963301672\n",
      "When time is 18350,get the best_k:10.702160129531316,and best_b:-44.5455727347199,the loss is 4.459099962493976\n",
      "When time is 18400,get the best_k:10.69413364731791,and best_b:-44.500513446182424,the loss is 4.458674807805578\n",
      "When time is 18600,get the best_k:10.66722969474888,and best_b:-44.32106680586652,the loss is 4.457034252835558\n",
      "When time is 18650,get the best_k:10.662703607792388,and best_b:-44.27679803116304,the loss is 4.456645598480291\n",
      "When time is 18700,get the best_k:10.655037204630341,and best_b:-44.234505541044534,the loss is 4.4562601672514655\n",
      "When time is 18750,get the best_k:10.647530880519652,and best_b:-44.194189335511005,the loss is 4.4559265235943215\n",
      "When time is 18800,get the best_k:10.642004793563109,and best_b:-44.15387312997748,the loss is 4.455596820843932\n",
      "When time is 18850,get the best_k:10.634266453642137,and best_b:-44.11395218136094,the loss is 4.455265840008421\n",
      "When time is 18900,get the best_k:10.626528113721166,and best_b:-44.07403123274441,the loss is 4.454934859172916\n",
      "When time is 18950,get the best_k:10.621567244155925,and best_b:-44.03371502721088,the loss is 4.454606281272153\n",
      "When time is 19000,get the best_k:10.614111512930604,and best_b:-43.99379407859435,the loss is 4.454275862861463\n",
      "When time is 19050,get the best_k:10.609150643365362,and best_b:-43.95347787306082,the loss is 4.453947284960698\n",
      "When time is 19100,get the best_k:10.601694912140042,and best_b:-43.913556924444286,the loss is 4.453616866550001\n",
      "When time is 19150,get the best_k:10.5967340425748,and best_b:-43.87324071891076,the loss is 4.45328828864924\n",
      "When time is 19200,get the best_k:10.58927831134948,and best_b:-43.833319770294224,the loss is 4.452957870238548\n",
      "When time is 19250,get the best_k:10.584317441784238,and best_b:-43.793003564760696,the loss is 4.452629292337786\n",
      "When time is 19300,get the best_k:10.576861710558918,and best_b:-43.75308261614416,the loss is 4.452298873927093\n",
      "When time is 19350,get the best_k:10.569426137436361,and best_b:-43.713556924444624,the loss is 4.451971620194024\n",
      "When time is 19400,get the best_k:10.563823765894837,and best_b:-43.67442648966208,the loss is 4.451658701829721\n",
      "When time is 19550,get the best_k:10.546058548503554,and best_b:-43.56691660823934,the loss is 4.450890096602194\n",
      "When time is 19600,get the best_k:10.541767639412644,and best_b:-43.53252925646074,the loss is 4.4506446287829995\n",
      "When time is 19650,get the best_k:10.536358548503543,and best_b:-43.498932418516134,the loss is 4.450414930986595\n",
      "When time is 19850,get the best_k:10.513962105815754,and best_b:-43.3649403236547,the loss is 4.449487292806364\n",
      "When time is 19900,get the best_k:10.507635228345396,and best_b:-43.33173874262709,the loss is 4.449269692623852\n"
     ]
    }
   ],
   "source": [
    "trying_times = 20000\n",
    "\n",
    "min_loss = float('inf')\n",
    "\n",
    "current_k = random.random() * 200 - 100\n",
    "current_b = random.random() * 200 - 100\n",
    "\n",
    "learning_rate = 1e-01\n",
    "\n",
    "for i in range(trying_times):\n",
    "    price_by_k_and_b = [price(r,current_k,current_b) for r in x_rm]\n",
    "    \n",
    "    current_loss = loss(y,price_by_k_and_b)\n",
    "    \n",
    "    if current_loss < min_loss:\n",
    "        min_loss = current_loss\n",
    "        best_k,best_b = current_k,current_b\n",
    "        \n",
    "        if i % 50 == 0:\n",
    "            print('When time is {},get the best_k:{},and best_b:{},the loss is {}'.format(i,best_k,best_b,min_loss))\n",
    "    \n",
    "    k_gradient = partial_k(x_rm,y,price_by_k_and_b)\n",
    "    b_gradient = partial_b(x_rm,y,price_by_k_and_b)\n",
    "    \n",
    "    current_k = current_k + (-1 * learning_rate) * k_gradient\n",
    "    current_b = current_b + (-1 * learning_rate) * b_gradient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x21e7c043cc0>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXEAAAD6CAYAAABXh3cLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJztnX+YXHV971+fmZ0kE2h3E4i3siQkSp9wGwikWZTbYBUQuJawRmhBooLKNVrKgxBvarhaSFBKIFbAtlxJa6posaxN2AKRBkio10RBsy5ZiUIFg8LS1iBsEFjIZvd7/5g5m9nZ8+N7zpwzc87M5/U8++zuzJlzvmdm930+5/NTjDEoiqIo2STX6AUoiqIo0VERVxRFyTAq4oqiKBlGRVxRFCXDqIgriqJkGBVxRVGUDKMiriiKkmFUxBVFUTKMiriiKEqGaUv6AEceeaSZO3du0odRFEVpKvr6+l4wxswK2i5xEZ87dy67du1K+jCKoihNhYj8wmY7dacoiqJkGBVxRVG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ogeJjyOBFxq9iDrQCuep59gctPI5HmOMmRX04sRFPEuIyC5jTFej15E0rXCeeo7NgZ5jMOpOURRFyTAq4oqiKBlGRXwiGxq9gDrRCuep59gc6DkGoD5xRVGUDKOWuKIoSoZREa9ARFaKyEONXkdSiMjJIvKciOwof81v9JqSQET+XEQeEZH7RWRKo9cTNyLyrorP8FkRuaTRa4obETlMRP5FRHaKyE2NXk8SiMgMEfm38jn+RdT9qIiXEZFjgKb7Z6hiBvB/jTGnlr+ebPSC4kZE3gIsMMacAtwPNN0sP2PMvzmfITAA9Dd6TQnwAeARY8wSYIGI/PdGLygBlgN7yue4RETmRdmJivghbgWubvQiEmYGcL6I/EBENomINHpBCXAGMENE/h/wDmBvg9eTGCIyHTjWGDPQ6LUkwBBwuIjkgSJwoMHrSQIBfqv8fyjASVF2oiIOiMhyYDfwk0avJWGeAv7CGPM24M3AOxu8niSYBewzxvwhJSv81AavJ0nOBLY1ehEJcTfwP4GngZ8aY55u8HqS4BtAB7AJeIPSxSo0KuIlllKy4P4JWCwilzd4PUnxDPBQxc9vathKkuNlwHET/RzobOBakuZc4L5GLyIhrqbk+psLzBSRP2jwepLiUmPMeZRE/FdRdqAiDhhjlpf9i+8H+owxf9PoNSXESuD9IpIDjgceb/B6kqAPcEqYj6Uk5E1H+Rb8XcD2Bi8lKX4LeL388xvA4Q1cS1L8IfBlEZlKyZXySJSdqIi3Fn8DfAR4FLjbGNN07iNjzPeBX4vID4EnjTE/aPSaEuJk4CfGmNcDt8wmfwv8qYh8n5KboRndRvcD04DvAp8zxrwSZSda7KMoipJh1BJXFEXJMCriiqIoGUZFXFEUJcOoiCuKomQYFXFFUZQMoyKuKIqSYVTEFUVRMsz/B0mQYVMMbtrPAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x,y = data['data'],data['target']\n",
    "x_rm = x[:,5]\n",
    "k = 10.507635228345396\n",
    "b = -43.33173874262709\n",
    "price_by_k_and_b = [price(r,k,b) for r in x_rm] # when k and b like this,get the smallest loss:4.449\n",
    "\n",
    "draw_rm_and_price()\n",
    "plt.scatter(x_rm,price_by_k_and_b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Part 3:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "solution = {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import lru_cache"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [],
   "source": [
    "@lru_cache(maxsize = 2 ** 10)\n",
    "def edit_distance(string1,string2):\n",
    "\n",
    "    if len(string1) == 0: return len(string2)\n",
    "    if len(string2) == 0: return len(string1)\n",
    "    \n",
    "    tail_s1 = string1[-1]\n",
    "    tail_s2 = string2[-1]\n",
    "    \n",
    "    candidates = [\n",
    "        (edit_distance(string1[:-1],string2) + 1,'DEL {}'.format(tail_s1)), # string 1 delete tail\n",
    "        (edit_distance(string1,string2[:-1]) + 1,'ADD {}'.format(tail_s2)), # string 1 add tail of string2\n",
    "    ]\n",
    "    \n",
    "    if tail_s1 == tail_s2:\n",
    "        both_forward = (edit_distance(string1[:-1],string2[:-1]) + 0, '')\n",
    "    else:\n",
    "        both_forward = (edit_distance(string1[:-1],string2[:-1]) + 1, 'SUB {} => {}'.format(tail_s1,tail_s2))\n",
    "    \n",
    "    candidates.append(both_forward)\n",
    "    \n",
    "    min_distance, operation = min(candidates,key = lambda x:x[0])\n",
    "    \n",
    "    solution[(string1,string2)] = operation\n",
    "    \n",
    "    return min_distance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "edit_distance('ABCDE','ABCCEF')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{('A', 'A'): '',\n",
       " ('A', 'AB'): 'ADD B',\n",
       " ('A', 'ABC'): 'ADD C',\n",
       " ('A', 'ABCC'): 'ADD C',\n",
       " ('A', 'ABCCE'): 'ADD E',\n",
       " ('A', 'ABCCEF'): 'ADD F',\n",
       " ('AB', 'A'): 'DEL B',\n",
       " ('AB', 'AB'): '',\n",
       " ('AB', 'ABC'): 'ADD C',\n",
       " ('AB', 'ABCC'): 'ADD C',\n",
       " ('AB', 'ABCCE'): 'ADD E',\n",
       " ('AB', 'ABCCEF'): 'ADD F',\n",
       " ('ABC', 'A'): 'DEL C',\n",
       " ('ABC', 'AB'): 'DEL C',\n",
       " ('ABC', 'ABC'): '',\n",
       " ('ABC', 'ABCC'): 'ADD C',\n",
       " ('ABC', 'ABCCE'): 'ADD E',\n",
       " ('ABC', 'ABCCEF'): 'ADD F',\n",
       " ('ABCD', 'A'): 'DEL D',\n",
       " ('ABCD', 'AB'): 'DEL D',\n",
       " ('ABCD', 'ABC'): 'DEL D',\n",
       " ('ABCD', 'ABCC'): 'SUB D => C',\n",
       " ('ABCD', 'ABCCE'): 'ADD E',\n",
       " ('ABCD', 'ABCCEF'): 'ADD F',\n",
       " ('ABCDE', 'A'): 'DEL E',\n",
       " ('ABCDE', 'AB'): 'DEL E',\n",
       " ('ABCDE', 'ABC'): 'DEL E',\n",
       " ('ABCDE', 'ABCC'): 'DEL E',\n",
       " ('ABCDE', 'ABCCE'): '',\n",
       " ('ABCDE', 'ABCCEF'): 'ADD F'}"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = [] # 记录字符串1转换到字符串2的步骤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [],
   "source": [
    "@lru_cache(maxsize = 2**10)\n",
    "def parse_solution(left_str,right_str):\n",
    "    '''parse the solution'''\n",
    "    if left_str == right_str: return \n",
    "   \n",
    "    tuple_str = left_str,right_str\n",
    "    operation = solution[tuple_str]\n",
    "    if operation:\n",
    "        result.append(left_str+' => '+right_str+' : '+operation)\n",
    "    \n",
    "    if not operation or operation[0] == 'S':\n",
    "        return parse_solution(left_str[:-1],right_str[:-1])\n",
    "    elif operation[0] == 'D':\n",
    "        return parse_solution(left_str[:-1],right_str)\n",
    "    elif operation[0] == 'A':\n",
    "        return parse_solution(left_str,right_str[:-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [],
   "source": [
    "parse_solution('ABCDE','ABCCEF')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['ABCDE => ABCCEF : ADD F', 'ABCD => ABCC : SUB D => C']"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import lru_cache"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "solution = {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [],
   "source": [
    "@lru_cache(maxsize = 2**10)\n",
    "def edit_distance_1(str1,str2):\n",
    "    '''another edit distance'''\n",
    "    if len(str1) == 0:\n",
    "        return len(str2)\n",
    "    elif len(str2) == 0:\n",
    "        return len(str1)\n",
    "    elif str1 == str2:\n",
    "        return 0\n",
    "    \n",
    "    if str1[-1] == str2[-1]:\n",
    "        d = 0\n",
    "    else:\n",
    "        d = 1\n",
    "    \n",
    "    return min(edit_distance(str1[:-1],str2) + 1,\n",
    "              edit_distance(str1,str2[:-1]) + 1,\n",
    "              edit_distance(str1[:-1],str2[:-1] )+ d)    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Part 4:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Fluent Python & Hands on Tensorflow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Part 5-1 Review machine learning"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Why do we use Derivative / Gredient to fit a target function?¶\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ans:通过有方向的监督学习减少计算次数，并通过导数的计算加快计算进程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. In the words 'Gredient Descent', what's the Gredient and what's the Descent?¶"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ans:我们的目标是找到使目标函数下降最快的方向，而最快的方向就是最陡峭的地方即最大的方向导数，这个方向导数就是梯度，下降是指为了使目标函数减少而使自变量朝着梯度相反的方向变化的意思"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. What's the advantages of the 3rd gradient descent method compared to the previous methods?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ans:有方向性，可以选择变化幅度从而减少大量运算量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. Using the simple words to describe: What's the machine leanring.¶"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ans: 机器学习是通过编程让计算机从数据中进行学习的科学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Part 5: Answer following questions: "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Why do we need dynamic programming? What's the difference of dynamic programming and previous talked `search` problme? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ans:1.动态规划是用来获取最优状态的一种方法。2.搜索算法是指每个阶段的最优状态是由之前所有阶段的状态的组合得到的；而动态规划是指每个阶段的最优状态可以从之前某个阶段的某个或某些状态直接得到而不管之前这个状态是如何得到的。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Why do we still need dynamic programming? Why not we train a machine learning to fit a function which could get the `right` answer based on inputs?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ans：动态规划是用来获取最优状态的一种算法，在机器学习应用中可以用来减少重复的计算量提高运行效率，与机器学习算法是相辅相成的"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. Can you catch up at least 3 problems which could solved by Dynamic Programming? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ans:有n级台阶有多少种走台阶的方法；给定数组arr返回arr的最长递增子序列；八皇后问题；Edit distance；背包问题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. Can you catch up at least 3 problems wich could sloved by Edit Distance? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ans:计算两个字串的相似度；检查拼写错误；网络搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. Please summarize the three main features of Dynamic Programming, and make a concise explain for each feature. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ans:最优子结构：每一个阶段最优问题的求解；边界：递推到根部时答案的确定；状态方程：从顶部从底部状态变化的方程"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6. What's the disadvantages of Dynamic Programming? (You may need search by yourself in Internet)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ans:无后效性（马尔科夫性）：如果某阶段状态给定后，则在这个阶段以后过程的发展不受这个阶段以前各段状态的影响；\n",
    "过程的过去历史只能通过当前的状态去影响它未来的发展；构造动态规划模型时，要充分注意是否满足无后效性的要求；"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Part 6 Preparation of Project-01"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Using python Flask or Bottle to finish your first simple web app:\n",
    "> https://bottlepy.org/\n",
    "\n",
    "2. Learn what's the SQL, and try some simple SQL operations:\n",
    "> https://www.w3schools.com/sql/sql_intro.asp\n",
    "\n",
    "3. Learn what's the HTML ( *ONLY* need to know the basic things)\n",
    "> https://getbootstrap.com/; https://www.w3schools.com/html/"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
